package com.bigfreakindeals.Protocols.SoapHandlers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.content.Intent;
import android.util.Log;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;
import com.bigfreakindeals.Models.ActiveDeal;
import com.bigfreakindeals.Models.BeansSays;
import com.bigfreakindeals.Models.UserProfile;

public class BeansSaysHandler  {
	
	static final String STATUS = "status";
	
	static Boolean isLogin;
	
	static final String USER_NAME = "username";
	static final String PASSWORD = "password";
	static final String SESSION_TOKEN = "sessionToken";

	static final  String ERROR = "error";
	
	UserProfile userProfile = new UserProfile();
	String feedUrl;
	 
    private static final String SOAP_ACTION = "1";
    private static final String METHOD_NAME = "bigfreakindeals";
    private static final String NAMESPACE = "1";
    
    
	public BeansSaysHandler(String feedUrl) {
	    	this.feedUrl = feedUrl;
	    	parse();
	}
	
	 public void parse() {

	     try {
		 
	    	SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
			SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		    envelope.setOutputSoapObject(request);
		
		    HttpTransportSE androidHttpTransport = new HttpTransportSE(feedUrl);

			androidHttpTransport.call(SOAP_ACTION, envelope);
			
			SoapObject response =(SoapObject)envelope.getResponse();
			boolean isActive =false;;
			
			String status  = response.getProperty(STATUS).toString();
	       		 if (status !=null && status.length() >0){
	       			 if (status.contentEquals("false")){
	       				isActive = false;
	       			 }else{
	       				isActive = true;
	       			 }
	       	}
	       		 
	       if(isActive){
	    		Vector beansaysItem = (Vector)response.getProperty("beans_comments");
				for (int count = 0;count <= beansaysItem.size() -1; count++){
					
					SoapObject beansSays = (SoapObject)beansaysItem.get(count);
					BeansSays.getInstance().clearBeansSay();
					if (beansSays.getProperty("id")!=null){
						String id  = beansSays.getProperty("id").toString();
						BeansSays.getInstance().setId(id);
						
					}
					
					if (beansSays.getProperty("created")!=null){
						String created  = beansSays.getProperty("created").toString();
						BeansSays.getInstance().setDatecreated(created);
					}
					
					if (beansSays.getProperty("title")!=null){
						String title  = beansSays.getProperty("title").toString();
						BeansSays.getInstance().setTitle(title);
					}
					
					if (beansSays.getProperty("comment")!=null){
						String comments  = beansSays.getProperty("comment").toString();
						BeansSays.getInstance().setComment(comments);
							
					}
					
					Intent intent = new Intent(Constant.EVENTS_GETBEANSSAYS); 
					ApplicationEx.getInstance().sendBroadcast(intent);
					return;
					
				}//end for
				
					
			}else{//no beans says avail
				//String error = userObj.getProperty(ERROR).toString();
				String errormessage = "No available Beans Says Today";
				Intent intent = new Intent(Constant.EVENTS_GETBEANSSAYS_ERROR); 
				intent.putExtra(Constant.TAG_WEBSERVICE_ERROR_MESSAGE, errormessage);
				ApplicationEx.getInstance().sendBroadcast(intent);
				
			}
			
	     } catch (IOException e1) {
				// TODO Auto-generated catch block
	    		
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
		
			} catch (Exception e1) {
				
				Intent intent = new Intent(Constant.EVENTS_HTTPCONNECTION_ERROR); 
				intent.putExtra(Constant.EVENTS_HTTPCONNECTION_MESSAGE, Constant.HTTPCONNECTION_ERROR_MESSAGE);
				ApplicationEx.getInstance().sendBroadcast(intent);
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	
	     
		
		
	 }
		
}